//
//  UISearchBar+EL.m
//  jobClient
//
//  Created by YL1001 on 2017/9/29.
//  Copyright © 2017年 YL1001. All rights reserved.
//

#import "UISearchBar+EL.h"

@implementation UISearchBar (EL)

- (void)searchFieldCornerRadius:(CGFloat)value ImageName:(NSString *)imageName{
    UIImage *backImg = [UIImage imageNamed:imageName];
    backImg = [backImg stretchableImageWithLeftCapWidth:backImg.size.width*0.5 topCapHeight:backImg.size.height*0.5];
    [self setSearchFieldBackgroundImage:backImg forState:UIControlStateNormal];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 13.0) {
        UITextField *searchField = [self valueForKey:@"_searchField"];
        searchField.layer.cornerRadius = value;
        searchField.layer.masksToBounds = YES;
    }
}

- (void)searchFieldPlaceHolderFontSize:(CGFloat)holderFontSize FontSize:(CGFloat)fontSize {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 13.0) {
        UITextField *searchField = [self valueForKey:@"_searchField"];
//        [searchField setValue:[UIFont systemFontOfSize:holderFontSize] forKeyPath:@"_placeholderLabel.font"];
        searchField.font = [UIFont systemFontOfSize:fontSize];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSFontAttributeName] = [UIFont systemFontOfSize:holderFontSize];
        NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:searchField.placeholder attributes:dict];
        [searchField setAttributedPlaceholder:attribute];
        
    }
}

- (void)setSearchBarBackImageYuanJiao{
    UIImage *backImg = [UIImage imageNamed:@"message_searchbar_back_img"];
    backImg = [backImg stretchableImageWithLeftCapWidth:backImg.size.width*0.5 topCapHeight:backImg.size.height*0.5];
    [self setSearchFieldBackgroundImage:backImg forState:UIControlStateNormal];
}

- (void)setSearchBarBackImage:(NSString *)image {
    UIImage *backImg = [UIImage imageNamed:image];
    backImg = [backImg stretchableImageWithLeftCapWidth:backImg.size.width*0.5 topCapHeight:backImg.size.height*0.5];
    [self setSearchFieldBackgroundImage:backImg forState:UIControlStateNormal];
}

- (void)setSearchBarBackImageDefault{
    UIImage *backImg = [UIImage imageNamed:@"message_searchbar_back_default"];
    backImg = [backImg stretchableImageWithLeftCapWidth:backImg.size.width*0.5 topCapHeight:backImg.size.height*0.5];
    [self setSearchFieldBackgroundImage:backImg forState:UIControlStateNormal];
}

-(void)setSearchBarPlaceholderFont:(CGFloat)holderFontSize{
    UITextField *searchField = [self valueForKey:@"_searchField"];
//    [searchField setValue:[UIFont boldSystemFontOfSize:holderFontSize] forKeyPath:@"_placeholderLabel.font"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
           dict[NSFontAttributeName] = [UIFont systemFontOfSize:holderFontSize];
           NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:searchField.placeholder attributes:dict];
           [searchField setAttributedPlaceholder:attribute];
}

- (void)setSearchBarIconWithELSearchIcon {
    [self setImage:[UIImage imageNamed:@"yewen_top_search"]
            forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
}

- (void)setSearchBarTextPosition {
    self.searchTextPositionAdjustment = UIOffsetMake(6,0);
}

- (void)configWithELStyle {
    [self searchFieldPlaceHolderFontSize:12.0 FontSize:13.0];
    [self setSearchBarIconWithELSearchIcon];
    
    self.searchTextPositionAdjustment = UIOffsetMake(6,0);
}

-(void)setMainRedColor {
    [self setBackgroundColor:EL_NavBarTintColor];
    [self setTintColor:EL_NavBarTintColor];
    [self setBarTintColor:EL_NavBarTintColor];
    if (iOS11) {
        [self setBackgroundImage:[[UIImage alloc] init]];
        [self setBackgroundColor:EL_NavBarTintColor];
    } else {
        [self setBackgroundImage:[UIImage createImageWithColor:EL_NavBarTintColor]];
    }
}

@end
